package com.jierui.meter.frontend.base.util;


import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 序列化：把对象转换为字节序列的过程称为对象的序列化
 * 反序列化：把字节序列恢复为对象的过程称为对象的反序列化
 *
 * 序列化和反序列化的工具类
 * @author jwang
 *
 */
@Slf4j
public class SerializeUtils
{
    /**
     * 序列化
     *
     * @param object
     * @return
     */
    public static byte[] serialize(Object object)
    {
        ObjectOutputStream objectOutputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try
        {
            // 序列化
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            byte[] bytes = byteArrayOutputStream.toByteArray();
            return bytes;
        }
        catch (Exception e)
        {
        	//TODO	        log.error("对象序列化失败！！！");
        	//TODO	        log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 反序列化
     *
     * @param bytes
     * @return
     */
    public static Object unserialize(byte[] bytes)
    {
        ByteArrayInputStream byteArrayInputStream = null;
        try
        {
            // 反序列化
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return objectInputStream.readObject();
        }
        catch (Exception e)
        {
        	//TODO	     	log.error("对象反序列化失败！！！");
        	//TODO	    	log.error(e.getMessage(), e);
        }
        return null;
    }

}
